import { Subscription } from 'rxjs';
import { currentFramework, adapter } from './adapter';
import { InitializationService } from './index';
import { PrividerService } from "./prividerService"
const Component = require('@angular/core').Component;

@Component(adapter.warpComponentMeta({
    styleUrls: ['./index.scss'],
    template: `
        <div  class="checkenv-container ">
            <header>
                <i class="material-icons back divvma" (click)="onBack()">arrow_back</i>
                <h1 class="padding">检测店铺配置环境</h1>
                <div class="nav">
                    <i ></i>
                    <i class="active">2</i>
                    <i ></i>
                    <i ></i>
                </div>
            </header>
            <div class="store margin" >
                <div *ngFor="let store of stores" (click)="selectStore(store)" class="store-info layout-row layout-center">
                    <img src={{store.merchant.logo}} onerror="this.onerror=null;this.src='./assets/img/logo-delivery.png'" />
                    <div>
                        <p>{{store.branchName}}</p>
                        <p><span>{{store.merchant.name}}</span></p>
                    </div>
                </div>
            </div>
        </div>
    `
}))
export default class CheckEnv {
    private checkStatus: string = "init";
    private system;
    private stores;
    private subscribetion: Subscription;
    private ldcPosTerminalName: string;

    constructor() {
        setTimeout(() => {
            this.init();
        }, 1);
    }
    onBack() {
        PrividerService.current.navigate(InitializationService.current.getPathByComponentName("connectLDC"))
    }
    async selectStore(store) {
        const ldcConfig = await PrividerService.current.fetchLDCConfig(store.id);
        PrividerService.current.doLocalstorageSetItem("preStore", store)
        PrividerService.current.doLocalstorageSetItem("preLDCConfig", ldcConfig)
        this.onNextClick();
    }

    private onNextClick() {
        PrividerService.current.navigate(InitializationService.current.getPathByComponentName("remoteLogin"))
    }
    private ngOnDestroy() {
        // this.subscribetion.unsubscribe()
    }
    async init() {
        const preLDCs = PrividerService.current.doLocalstorageGetItem("preLDCs");
        const promises = preLDCs.map(async (x) => {
            try {
                return await PrividerService.current.getStore(x.storeId)
            } catch (error) {
                return null;
            }

        });
        this.stores = (await Promise.all(promises)).filter(x => x);
    }
}


